#!/usr/bin/env python
# encoding: utf-8
"""
gen_utils.py
代码生成器工具

Created by rikugun on 2010-08-12.
Copyright (c) 2010 TYDIC. All rights reserved.
"""

import sys
import os
import optparse
from string import Template
from time import ctime

def new_trans():
	"""生成新的PROC交易文件,适用于BSS CRM系统的普通交易.
	模板请参考templates目录下文件
	"""
	opt = optparse.OptionParser(usage ='new_trans [option] trans_name',version='0.1')
	opt.add_option('-t','--template',help=u'自定义模板')
	opt.add_option('--author',help='Author',default=os.getenv('USER'))
	opt.add_option('-d','--desc',help=u'交易描述',default='new trans')
	opt.add_option('--ver',help=u'交易版本',default='0.1')
	opt.add_option('--create_date',help=u'创建时间',default=ctime())
	opt.add_option('-I','--integrate',help=u'创建综合交易',action='store_true')
	opts,args = opt.parse_args()
	if len(args) == 0 :
		opt.print_help()
		exit(2)
	tp_name = '%s_trans.inc' % ('integrate' if opts.integrate else 'atom')
	tp_name =opts.template or os.path.join(os.path.dirname(os.path.realpath(__file__)),'templates',tp_name)
	print tp_name
	# exit(2)

	trans_name = args[0]+'A' if not opts.integrate else args[0]
	with open(trans_name+'.pc','w') as fp:
		tmp = open(tp_name,'r')
		fp.write(Template(tmp.read()).safe_substitute(dict(
					author = opts.author,
					trans_name = trans_name,
					ver = opts.ver,
					create_date = opts.create_date,
					desc = opts.desc
				)))
		tmp.close()
	print os.path.realpath(trans_name+'.pc'),u' 已创建!'
	
if __name__ == '__main__':
	new_trans()


